package random;

import java.util.Random;

/**
 * 需求:通过26个大小写字母以及0-9数字,随机生成四位
 * 1.构造数据:26个大小写字母以及0-9数字
 * 2.从以上数据中随机取四位
 *         0 1 2 3
 *  char[] a b c d //索引和元素是一一对应,索引随机也代表元素随机
 *
 */
public class RandomDemo02 {
    public static void main(String[] args) {
        //method01();
        //method02();
        StringBuilder data = new StringBuilder();
        char ch;
        for(ch = 'a';ch<='z';ch++){
            data.append(ch);
        }
        for(ch = 'A';ch<='Z';ch++){
            data.append(ch);
        }
        for(ch = '0';ch<='9';ch++){
            data.append(ch);
        }
        System.out.println(data);
        //method01(data);
        String datastr = data.toString();
        StringBuilder randomcode = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int randomindex = new Random().nextInt(datastr.length());
            randomcode.append(datastr.charAt(randomindex));
        }
        System.out.println(randomcode);

    }

    private static void method01(StringBuilder data) {
        char[] chars = data.toString().toCharArray();
        StringBuilder randomcode = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int randomindex = new Random().nextInt(chars.length);
            randomcode.append(chars[randomindex]);
        }
        System.out.println(randomcode);
    }

    private static void method02() {
            char ch = 'a';
            ch = (char)(ch + 1);
            System.out.println(ch);
        }
        private static void method01() {
            char ch = 'a';
            ch++;//ch = ch+1
            // ch = 'a' + 1 //自动类型提升 a字符的ASCII码 97
            // ch = (char)(97 + 1) //自动类型强转(编译器帮我们实现的)
            // ch = 'b'
            System.out.println(ch);//'b'
        }
    }

